<?php

namespace App\Models;


class ImSessionInfo extends BaseModel
{
    protected $table = 'im_session_info';//会话信息表
    const WEB_LOGIN_CAPTCHA = 1;//web 登录验证码

    /**
     * 会话设置
     * @param $stype 会话类型
     * @param $session_id 会话ID
     * @param $session 会话内容
     * @param int $expired_at 过期时间，十位数的秒级时间戳
     * @author an
     * @time 2022-01-05 9:20
     */
    public function setSession($stype, $session_id, $session, $expired_at = 0)
    {
        $sessionObj = $this->where('session_id', $session_id)->first();
        if (!$sessionObj) {
            $sessionObj = new self();
            $sessionObj->session_id = $session_id;
        }
        $sessionObj->stype = $stype;
        $sessionObj->session = $session;
        $sessionObj->expired_at = $expired_at;
        $sessionObj->save();
    }

    /**
     * 获取会话信息
     * @param $stype 会话类型
     * @param $session_id 会话ID
     * @author an
     * @time 2022-01-05 9:20
     */
    public function getSession($session_id, $stype)
    {
        $session = $this->where('stype', $stype)
            ->where('session_id', $session_id)
            ->first();
        if (!$session || ($session->expired_at > 0 && $session->expired_at < time())) {
            return null;
        }
        return $session->session;
    }

    /**
     * 后台图形验证码会话存储
     * @param $session_id
     * @param $session
     * @param int $expired_at
     * @author an
     * @time 2022-01-05 9:19
     */
    public function setCaptChaSession($session_id, $session, $expired_at = 0)
    {
        $this->setSession(self::WEB_LOGIN_CAPTCHA, $session_id, $session, $expired_at = 0);
    }

    /**
     * 获取后台图形验证码
     * @param $session_id
     * @author an
     * @time 2022-01-05 9:37
     */
    public function getCaptChaSession($session_id)
    {
        $session = $this->getSession($session_id, self::WEB_LOGIN_CAPTCHA);
        return $session;
    }
}
